<template>
  <div class="good-item" @click="showdetail">
    <img :src="showImage" alt="" @load="imgload" />
    <div class="good-info">
      <p>{{ goodmessage.title }}</p>
      <span class="good-price">{{ countprice }}</span>
      <span class="good-cfav">{{ goodmessage.cfav }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "gooditem",
  props: {
    goodmessage: {
      type: Object,
      default() {
        return [];
      },
    },
  },
  computed: {
    countprice() {
      return "￥" + this.goodmessage.price;
    },
    showImage() {
      return this.goodmessage.image == null
        ? this.goodmessage.show.img
        : this.goodmessage.image;
    },
  },
  methods: {
    imgload() {
      // console.log("图片加载成功");
      if (this.$route.path.indexOf("/home") != -1) {
        //判断当前路由是home还是detail  发送不同的事件
        this.$bus.$emit("homeimgload");
      } else if (this.$route.path.indexOf("/detail") != -1) {
        this.$bus.$emit("detailimgload");
      }
    },
    showdetail() {
      this.$router.push({
        path: "/detail",
        query: {
          //通过动态路由来传递参数
          id: this.goodmessage.iid,
        },
      });
    },
  },
};
</script>

<style>
.good-item {
  width: 47%;
  position: relative;
  padding-bottom: 2rem;
}
.good-item img {
  width: 100%;
  height: 319px;
  border-radius: 5px;
}
.good-info {
  width: 90%;
  font-size: 15px;
  position: absolute;
  bottom: 5px;
  left: 12px;
  right: 0;
  overflow: hidden;
  text-align: center;
}
.good-info p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 3px;
}
.good-info .good-price {
  color: var(--color-high-text);
  margin-right: 20px;
}
.good-info .good-cfav {
  position: relative;
}
.good-info .good-cfav::before {
  content: "";
  position: absolute;
  left: -15px;
  top: 1px;
  width: 0.6rem;
  height: 14px;
  background: url(../../../assets/img/common/collect.svg) 0 0/14px 14px;
  /* background-size: 100% 100%; */
  /* background-position-y: 2px; */
}
</style>